$(function () {
	// viewTable();
	viewfield();

	//选择模块名称下拉
	$('.drop-input').click(function(){
		viewModalName();
		$('.drop-list').slideToggle();
	})

	// 选择模块名称
	$('.drop-down').on('click','.drop-list li',function(){
		var modalName = $(this).text();
		$('.drop-input').val(modalName);
		$('.drop-list').fadeOut();
		$('.modal-id-input').val($(this).next('input').val());
		//viewTable();
		viewfield();
	})

	//增加字段
	$('.key-div').on('click','.key-word',function(){
		var key = $(this).text();
		var id = $(this).next('input').val();
		var num = $('.key-tr').length + 1;
		$('.key-body').append('<tr class="key-tr">'+
									'<td>'+num+'</td>'+
									'<td class="field-name">'+key+'</td>'+
									'<td>'+
										'<i class="iconfont icon-shangla"><input type="hidden" name="" value="{{id}}"></i> &nbsp;&nbsp;'+
            							'<i class="iconfont icon-xiala"></i> &nbsp;&nbsp;'+
            							'<i class="iconfont icon-lajitong"></i>'+
            						'</td>'+
								'</tr>')
		$(this).remove();
	})

	//删除字段
	$('.key-body').on('click','.icon-lajitong',function(){
		var key = $(this).parent('td').prev().text();
		var id = $(this).children('input').val();
		$('.key-div').append('<p class="key-word">'+
								key+'</p>'+
    							'<input type="hidden" name="" value="'+id+'">')
		$(this).parents('tr').first().remove();
	})

	//下移顺序
	$('.key-body').on('click','.icon-xiala',function(){
		var a = $(this).index('.icon-xiala');
		var len = $('.key-tr').length;
		var thisKey = $('.key-tr').eq(a%len).children('td:eq(1)').html();
		var thisId = $(this).children('input').val();

		var nextKey = $('.key-tr').eq((a+1)%len).children('td:eq(1)').html();
		var nextId = $('.icon-lajitong').eq((a+1)%len).children('input').val();

		$('.key-tr').eq(a%len).children('td:eq(1)').text(nextKey);
		$(this).children('input').val(thisId);

		$('.key-tr').eq((a+1)%len).children('td:eq(1)').text(thisKey);
		$('.icon-lajitong').eq((a+1)%len).children('input').val(thisId)
	})

	//上移顺序
	$('.key-body').on('click','.icon-shangla',function(){
		var a = $(this).index('.icon-shangla');
		var len = $('.key-tr').length;
		var thisKey = $('.key-tr').eq(a%len).children('td:eq(1)').html();
		var thisId = $(this).children('input').val();

		var nextKey = $('.key-tr').eq((a-1)%len).children('td:eq(1)').html();
		var nextId = $('.icon-lajitong').eq((a-1)%len).children('input').val();

		$('.key-tr').eq(a%len).children('td:eq(1)').text(nextKey);
		$(this).children('input').val(thisId);

		$('.key-tr').eq((a-1)%len).children('td:eq(1)').text(thisKey);
		$('.icon-lajitong').eq((a-1)%len).children('input').val(thisId)
	})

	//保存状态
	$('.save-btn').click(function(){
		var field = [];
		for(var i=0;i<$('.key-tr').length;i++){
			field.push($('.field-name').eq(i).text())
		}
		console.log(field + " id =" +$('.modal-id-input').val());
		// console.log(JSON.stringify(field));
		var options = getBASEPUTAJAX();
		options.url = "/epi/admin/view/columns/" + $('.modal-name').val();
		
		options.data.field = field;
		//最好再传一个modelname
		options.success = function(data) {
   			//200为添加成功
			// 400为添加失败
			// 500为非法操作
			var status = data.status;
			if(status == 500){
				setAlert('非法操作');
			}else if(status == 400){
				setAlert('保存失败');
			}else if(status == 200){
				setAlert('保存成功！');
			}else{
				setAlert('系统繁忙，请刷新重试');
			}
            
        };
        options.error = function() {
            setAlert('系统繁忙，请刷新重试');
        };
		$.ajax(options);
		
	})
})

/*>>>>>>> 145197e9b2128dece48d865c603d379eece0eb0f*/
/**
 * [viewfield 显示数据]
 */
function viewfield(){
	console.log($('.modal-id-input').val());
	viewModalName(function(){
		var options = getBASEGETAJAX();
		options.url = "/epi/admin/view/columns/" + $('.modal-name').val();
		options.success = function(data) {
			console.log(data);
	    	var checked = '';
	    	var unchecked = '';
	    	for(var i=0;i<data.length;i++){
	    		if(data[i].view.checked != true){
	    			unchecked += '<p class="key-word">'+data[i].view.fieldname+'</p>'+
								'<input type="hidden" name="" value="'+data[i].view.id+'">'
	    		}else{
	    			checked += '<tr class="key-tr">'+
							        '<td>'+data[i].view.number+'</td>'+
							        '<td class="field-name">'+data[i].view.fieldname+'</td>'+
							        '<td>'+
							            '<i class="iconfont icon-shangla field-id"><input type="hidden" name="" value="'+data[i].id+'"></i> &nbsp;&nbsp;'+
							            '<i class="iconfont icon-xiala"><input type="hidden" name="" value="'+data[i].id+'"> </i> &nbsp;&nbsp;'+
							            '<i class="iconfont icon-lajitong"><input type="hidden" name="" value="'+data[i].id+'"> </i>'+
							        '</td>'+
							    '</tr>'
	    		}
	    	}
	        $('.key-div').html(unchecked);
	        $('.key-body').html(checked);
	    };
	    options.error = function() {
	        setAlert('系统繁忙，请刷新重试');
	    };
		$.ajax(options);
	})
	
}

/**
 * [viewModalName 功能列表]
 */
function viewModalName(callback){
	$.ajax({
        url: '/epi/admin/view/models',//"/epi/admin/view/columns",
        type: "GET",
        dataType: 'json',
        success: function(data) {
        	if(data.modelNames.length >0){
            	$('.modal-name').val(data.modelNames[0]);
        	}
        	console.log(JSON.stringify(data))
            var myTemplate = Handlebars.compile($("#modalList-template").html());
            $('.drop-list').html(myTemplate(data));
            callback();
        },
        error: function() {
            setAlert('系统繁忙，请刷新重试');
        }
    });
}


